package com.onstar.cn.framework.common;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.core.BaseFragmentActivity;
import com.onstar.cn.weight.discover.ItemViewData;
import com.onstar.cn.weight.more.CustomLineLayout;
import com.onstar.cn.weight.more.ImageIconTextLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by 7828wc on 16/6/30.
 * 处理一些公用控件，封装一些控件自动跳转Fragment或者Activity
 * <br/>如果不是跳转Fragment或者Activity无需设置ItemViewData.setClassName;需自行设置setOnViewItemClickListener进行监听
 */
public class CustomLayoutsGroup {
    private List<ItemViewData> itemViewDatas;
    private Context context;
    private OnViewItemClickListener onViewItemClickListener;

    public CustomLayoutsGroup(Context context, List<ItemViewData> datasLayout) {
        this.context = context;
        this.itemViewDatas = datasLayout;
    }

    public CustomLayoutsGroup(Context context) {
        this.context = context;
    }

    public void addLayouts(List<ItemViewData> layouts) {
        this.itemViewDatas = layouts;
    }

    public void addLayout(ItemViewData layout) {
        if (itemViewDatas != null && itemViewDatas.size() > 0) {
            itemViewDatas.add(layout);
        } else {
            itemViewDatas = new ArrayList<ItemViewData>();
        }
        addLayouts(itemViewDatas);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ItemViewData itemViewData = itemViewDatas.get((int) view.getTag());
            boolean isJump = false;
            if (onViewItemClickListener != null) {
                isJump = onViewItemClickListener.onViewItemClick(itemViewData);
            }
            if (!CommonUtil.isNull(itemViewData.getClassName()) && isJump) {
                itemViewDatas.get((int) view.getTag()).setJump(true);
                if (context instanceof BaseFragmentActivity) {
                    BaseFragmentActivity activity = (BaseFragmentActivity) context;
                    try {
                        Class<?> clazz = Class.forName(itemViewData.getClassName());
                        if (clazz.newInstance() instanceof Fragment) {
                            Fragment fragment = (Fragment) clazz.newInstance();
                            if (!CommonUtil.isNull(itemViewData.getBundle())) {
                                fragment.setArguments(itemViewData.getBundle());
                            }
                            activity.changeFragment(fragment, clazz, false);
                        } else {
                            Intent intent = new Intent(context, clazz);
                            if (!CommonUtil.isNull(itemViewData.getBundle())) {
                                intent.putExtras(itemViewData.getBundle());
                            }
                            context.startActivity(intent);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    };

    public void customLineLayoutGroup(ViewGroup parentLayout, List<ItemViewData> itemViews) {
        this.itemViewDatas = itemViews;
        customLineLayoutGroup(parentLayout);
    }

    public void customLineLayoutGroup(ViewGroup parentLayout) {
        for (int i = 0; i < itemViewDatas.size(); i++) {
            ItemViewData itemViewData = itemViewDatas.get(i);
            CustomLineLayout customLineLayout = new CustomLineLayout(context);
            customLineLayout.setText(itemViewData.getText());
            if (!CommonUtil.isNull(itemViewData.getIcon())) {
                customLineLayout.setImageIcon(itemViewData.getIcon());
            }
            customLineLayout.setTag(i);
            customLineLayout.showRightArrow(itemViewData.isShowRightArrow());
            customLineLayout.setRightText(itemViewData.getRightText());
            customLineLayout.setOnClickListener(onClickListener);
            parentLayout.addView(customLineLayout);
        }
    }

    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

    public void customShortCutsLayoutGroup(ViewGroup parentLayout, List<ItemViewData> itemViews) {
        this.itemViewDatas = itemViews;
        for (int i = 0; i < itemViewDatas.size(); i++) {
            ItemViewData itemViewData = itemViewDatas.get(i);
            ImageIconTextLayout customLineLayout = new ImageIconTextLayout(context);
            String txt = itemViewData.getText();
            if (!CommonUtil.isNull(txt))
                customLineLayout.setText(isInteger(txt) ? context.getString(Integer.parseInt(txt)) : txt);
            if (!CommonUtil.isNull(itemViewData.getIcon())) {
                customLineLayout.setIcon(itemViewData.getIcon());
            }

            DisplayMetrics dm = new DisplayMetrics();
            ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(dm.widthPixels / 5, ViewGroup.LayoutParams.WRAP_CONTENT);
            customLineLayout.setLayoutParams(params);
            customLineLayout.setTag(i);
            customLineLayout.setOnClickListener(onClickListener);
            parentLayout.addView(customLineLayout);
        }
    }

    public void setOnViewItemClickListener(OnViewItemClickListener onViewItemClickListener) {
        this.onViewItemClickListener = onViewItemClickListener;
    }

    public interface OnViewItemClickListener {
        /**
         * Need return true,to preform thr operations
         *
         * @param item
         * @return
         */
        boolean onViewItemClick(ItemViewData item);
    }
}
